<template>
  <j-modal
    :title="title"
    :width="1000"
    :visible="visible"
    :maskClosable="false"
    :ok-button-props="{ props: { disabled: disableSubmit } }"
    @close="close"
    cancelText="关闭"
  >
    <div style="margin: 0 auto" :class="{ isCheck: isCheck }">
      <a-form-model ref="form" :model="model" :rules="rules" :label-col="labelCol" :wrapper-col="wrapperCol">
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="底稿编号">
              {{ model.dgbh ? model.dgbh : '系统生成' }}
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="索引号">
              {{ model.syh ? model.syh : '系统生成' }}
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="问题分类" prop="quatypes">
              <a-tree-select
                :replaceFields="{ children: 'children', title: 'name', key: 'id', value: 'id' }"
                v-model="model.quatypes"
                style="width: 100%"
                :tree-data="treeData"
                :disabled="disableSubmit"
                :maxTagCount="2"
                search-placeholder="请选择问题分类"
                treeNodeFilterProp="title"
              />
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="被审计单位">
              {{ bsjdwName || model.bsjdwName }}
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="审计事项">
              <a-input v-model='model.sjsx' :disabled='disableSubmit' />
<!--              <a-tree-select
                :replaceFields="{ children: 'children', title: 'itemName', key: 'id', value: 'id' }"
                treeDefaultExpandAll
                v-model="model.sjsx"
                style="width: 100%"
                :tree-data="treeData1"
                :disabled="disableSubmit"
              />-->
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="审计人" prop="sjrName">
              {{ nickname() }}
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="审计时间">
              <j-date style="width: 100%" v-model="model.sjsj" :disabled="disableSubmit"></j-date>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-form-model-item label="附件上传" :label-col="{ span: 2 }" :wrapper-col="{ span: 20 }">
            <RuleUpload
              :isShow="disableSubmit"
              ref="fileUpload"
              :fileid="model.fileId"
              @uploadFile="uploadFile"
            ></RuleUpload>
          </a-form-model-item>
          <a-form-model-item label="审计过程" :label-col="{ span: 2 }" :wrapper-col="{ span: 20 }">
            <a-textarea
              allow-clear
              v-model="model.sjgc"
              :disabled="disableSubmit"
              :auto-size="{ minRows: 2, maxRows: 5 }"
            />
          </a-form-model-item>
          <a-form-model-item label="问题描述" :label-col="{ span: 2 }" :wrapper-col="{ span: 20 }">
            <a-textarea
              allow-clear
              v-model="model.wtms"
              :disabled="disableSubmit"
              :auto-size="{ minRows: 2, maxRows: 5 }"
            />
          </a-form-model-item>
          <a-form-model-item label="定性" :label-col="{ span: 2 }" :wrapper-col="{ span: 20 }">
            <a-textarea
              allow-clear
              v-model="model.dingxing"
              :disabled="disableSubmit"
              :auto-size="{ minRows: 2, maxRows: 5 }"
            />
          </a-form-model-item>
          <a-form-model-item label="定性依据" :label-col="{ span: 2 }" :wrapper-col="{ span: 20 }" prop="dxyj">
            <a-select
              v-model="model.dxyj"
              mode="tags"
              style="width: 100%"
              option-label-prop="label"
              :disabled="disableSubmit"
            >
              <template v-for="(item, index) in dxyjList">
                <a-select-option :value="item.title || ''" :label="item.title" :key="item.id">
                  {{ item.title }}
                </a-select-option>
              </template>
            </a-select>
          </a-form-model-item>
          <a-form-model-item label="涉及金额" :label-col="{ span: 2 }" :wrapper-col="{ span: 20 }">
            <a-textarea
              allow-clear
              v-model="model.tjsjje"
              :disabled="disableSubmit"
              :auto-size="{ minRows: 2, maxRows: 5 }"
            />
          </a-form-model-item>
          <a-form-model-item label="责任分类" :label-col="{ span: 2 }" :wrapper-col="{ span: 20 }">
            <j-dict-select-tag
              style="width: 100%"
              v-model="model.glfl"
              dict-code="digaoglfl"
              :disabled="disableSubmit"
            />
          </a-form-model-item>
          <a-form-model-item label="责任界定" :label-col="{ span: 2 }" :wrapper-col="{ span: 20 }">
            <a-textarea
              allow-clear
              v-model="model.zrjd"
              :disabled="disableSubmit"
              :auto-size="{ minRows: 2, maxRows: 5 }"
            />
          </a-form-model-item>
          <a-form-model-item label="整改建议" :label-col="{ span: 2 }" :wrapper-col="{ span: 20 }">
            <a-textarea
              allow-clear
              v-model="model.zgjy"
              :disabled="disableSubmit"
              :auto-size="{ minRows: 2, maxRows: 5 }"
            />
          </a-form-model-item>
        </a-row>
      </a-form-model>
    </div>
    <!-- table区域-end -->
  </j-modal>
</template>
<script>
import { httpAction, getAction } from '@/api/manage'
import RuleUpload from '../../../../components/RuleUpload'
import { mapGetters } from 'vuex'
import { currentYMD } from '@/mixins/commontime.js'
export default {
  name: 'SysPositionModal',
  props: ['id', 'bsjdwName', 'bsjdw', 'protype', 'type'],
  components: {
    RuleUpload,
  },
  data() {
    return {
      labelCol: { span: 4 },
      wrapperCol: { span: 18 },
      disableSubmit: false,
      title: '',
      visible: false,
      isCheck: false,
      model: {},
      dxyjList: [],
      treeData: [],
      treeData1: [],
      rules: {
        quatypes: [{required: true, message: '不能为空!'}],
        itemName: [{ required: true, message: '不能为空!' }],
        dxyj: [{ required: true, message: '不能为空!' }],
      },
      url: {
        dxyjList: '/sjzszhly/knowledgeRegulation/queryKnowledgeRegulation',
        add: '/sjxmzygl/sjWorkZtfaEveDigao/add',
        edit: '/sjxmzygl/sjWorkZtfaEveDigao/edit',
        tree: '/sys/category/problemTreeBytype',
        tree1: '/sjxmzygl/sjWorkZtfaEve/getMyWorkZtfaEveList',
      },
    }
  },
  created() {
    // this.getDxyjList()
    // this.loadTree()
    // this.loadTree1()
  },
  watch: {},
  methods: {
    ...mapGetters(['nickname']),
    loadTree1() {
      this.treeData1 = []
      getAction(this.url.tree1, {
        proId: this.id || this.model.proId,
        typeId: this.type || this.model.categoryType,
      }).then((res) => {
        if (res.success) {
          this.treeData1 = res.result
        }
      })
    },
    add() {
      this.model = {
        bsjdwName: this.bsjdwName,
        bsjdw: this.bsjdw,
        sjsj: currentYMD(),
      }
      this.getDxyjList()
      this.loadTree()
      this.loadTree1()
      this.visible = true
    },
    check(record) {
      this.model = Object.assign(
        {
          bsjdwName: this.bsjdwName,
          bsjdw: this.bsjdw,
        },
        record
      )
      this.getDxyjList()
      this.loadTree()
      this.loadTree1()
      if (this.model.dxyj) {
        this.model.dxyj = this.model.dxyj.split(',')
      } else {
        this.model.dxyj = []
      }
      if (this.model.quatypes) {
        this.model.quatypes = this.model.quatypes.split(',')
      } else {
        this.model.quatypes = []
      }
      this.visible = true
    },
    edit(record) {
      this.model = Object.assign(
        {
          bsjdwName: this.bsjdwName,
          bsjdw: this.bsjdw,
        },
        record
      )
      this.getDxyjList()
      this.loadTree()
      this.loadTree1()
      if (this.model.dxyj) {
        this.model.dxyj = this.model.dxyj.split(',')
      } else {
        this.model.dxyj = []
      }
      if (this.model.quatypes) {
        this.model.quatypes = this.model.quatypes.split(',')
      } else {
        this.model.quatypes = []
      }
      this.visible = true
    },
    getDxyjList() {
      getAction(this.url.dxyjList).then((res) => {
        if (res.success && res.result.length > 0) {
          this.dxyjList = res.result
        } else {
          console.log(res)
        }
      })
    },
    close(isSubmit) {
      if (isSubmit) {
        this.saveData()
      } else {
        this.visible = false
      }
    },
    saveData() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.$refs.fileUpload.saveFile('sjdg')
        } else {
          return false
        }
      })
    },
    loadTree() {
      this.treeData = []
      getAction(this.url.tree, { protype: this.protype || this.model.categoryType }).then((res) => {
        if (res.success) {
          this.treeData = res.result[0].children
          this.treeData.forEach(item => item.selectable = false)
        }
      })
    },
    uploadFile(id) {
      console.log(this.model)
      this.model.fileId = id
      this.$refs.form.validate((valid) => {
        if (valid) {
          let url, type
          if (!this.model.id) {
            url = this.url.add
            type = 'post'
          } else {
            url = this.url.edit
            type = 'put'
          }
          this.model.dxyj = this.model.dxyj.toString()
          this.model.quatypes = this.model.quatypes.toString()
          httpAction(url, { proId: this.id || this.model.proId, ...this.model }, type).then((res) => {
            if (res.success) {
              this.$message.success(res.message)
              this.$emit('ok')
              this.visible = false
            } else {
              this.$message.error(res.message)
            }
          })
        } else {
          return false
        }
      })
    },
  },
}
</script>

<style lang="less" scoped>
@import '../../../../css/checkForm.less';

.name-type {
  width: 110px;
  margin-left: 5px;
  border: 1px solid #e0e0e0;

  /deep/ .ant-select-selection {
    box-sizing: border-box;
    min-height: 27px !important;

    .ant-select-selection__rendered {
      line-height: 27px !important;
    }
  }
}
</style>
